package com.apes.scm.masterdata.party.service;


import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.area.repository.AreaRepository;
import com.apes.scm.masterdata.party.model.Corporate;
import com.apes.scm.masterdata.party.model.Party;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("corporateSerive")
public class CorporateSerive {

    @Autowired
    AreaRepository areaRepository;


    // 设置注册省份、注册城市、办公省份、办公城市
    public void setCorporateCityAndProvince(Party party){
        Corporate corporate = party.getCorporate();
        // 获取注册区县
        Area regCounty = corporate.getRegCounty();
        // 获取注册城市编码
        String regCityId = regCounty.getParent();
        // 查找注册城市
        Area regCity = areaRepository.findOne(regCityId);
        // 设置注册城市
        corporate.setRegCity(regCity);
        String regProvinceId = regCity.getParent();
        Area regProvince = areaRepository.findOne(regProvinceId);
        corporate.setRegProvince(regProvince);
        // 获取注册区县
        Area officeCounty = corporate.getOfficeCounty();

        String officeCityId = officeCounty.getParent();

        if (regCityId.equals(officeCityId)) {
            corporate.setOfficeCity(regCity);
            corporate.setOfficeProvince(regProvince);
        } else {
            Area officeCity = areaRepository.findOne(officeCityId);
            corporate.setOfficeCity(officeCity);
            String officeProvinceId = officeCity.getParent();
            Area officeProvince= areaRepository.findOne(officeProvinceId);
            corporate.setOfficeProvince(officeProvince);
        }

    }



}
